Analysis and Solutions of Common Causes of Serial Port Lost Data

Published: 15 August 2022 | Last Updated: 15 August 20223765
Hello everyone, I am Rose. Welcome to the new post today. Today I will introduce analysis and solutions of common causes of serial port lost data.
This video will introduces the knowledge of UART.

Understanding UART

Topics covered in this article:
Ⅰ. UART several flags
Ⅱ. UART communication hardware interface
Ⅲ. UART receive lost data
Ⅳ. UART sends lost data


UART stands for Universal Asynchronous Receiver/Transmitter, also known as what we often refer to as a serial port.

One of the serial peripherals that engineers utilize the most frequently is the serial port, but real-world applications frequently run into a variety of issues. One byte of data, for instance, is gone.

Today, we'll discuss STM32 and UART-related topics, as well as the issue of quickly losing one byte of data.

Ⅰ. UART several flags

Here, we'll concentrate on the TXE, TC, RXNE, and ORE flag bits found in the UART status register.

Figure. 1.png

Figure. 1

Programmers frequently use these flag bits. These flag bits might not matter to students who only utilize the function library indirectly. Inadequate use of them could result in data loss.

TXE: Transmit data register empty

0: Data not transferred to shift register

1: Data transfer to shift register

TC: Transmission complete

0: Transmission not completed

1: Transmission completed

RXNE: Read data register not empty

0: No data received

1: Ready to read received data

ORE: Overrun error

0: no overflow error

1: An overflow error was detected

 

Ⅱ. UART communication hardware interface

Common UART communication hardware interfaces include: TTL, RS232, and RS485.

When programming, the communication interface technique must be taken into account. The line's latency needs to be taken into account while communicating across vast distances. Loss of data will also result from improper operation.

1. TTL

TTL is relatively simple, that is, it is directly connected to the Tx and Rx pins of the UART without external conversion. As shown in the figure:

Figure. 2.png

Figure. 2

Note: Tx and Rx pins need to be cross connected.

2. RS232

One of the most widely used serial communication interface standards is the RS-232 standard interface, which specifies that the level of logic "1" is -5V to 15V and the level of logic "0" is +5V to 15V.

This electrical standard was chosen to expand communication range and enhance anti-interference performance.

Figure. 3.jpg

Figure. 3

3. RS485

RS485 typically uses a two-wire wiring approach. There can be connections between several nodes on a single bus in this wiring topology.

Ordinary twisted pair cables can be used in low-speed, short-distance, interference-free situations; however, for high-speed, long-distance transmission, RS485 special cables with impedance matching (typically 120 ohms) are required. Armored twisted pair shielded cable should also be used.

 Figure. 4.jpg

Figure. 4


Ⅲ. UART receive lost data

The loss of data received by the UART may be related to both software and hardware. Here are some common causes and solutions for lost data.

1. Receive overflow lost data

refers to data loss as a result of an overflow error caused by a delay in taking data, which typically happens when a lot of data is received and data is obtained by a query. Too much data is collected and processed slowly during the MCU's initialization procedure, delaying the response of the complicated system. Loss of data will happen.

Solution:

Clear overflow error flag in time

Utilize communication protocols to filter problems caused by data loss

2. Receive interrupt lost data

The UART interrupt is more frequently used to receive data than to query that data. Although the interrupt method replies faster than the query method, it will still lose data if it is processed excessively.

When there is a lot of data, the UART receive interrupt function takes a long time, has a low priority, and it is simple to lose data.

Solution:

Reduce unnecessary time-consuming in the interrupt function

Reasonable allocation of interrupt priority

Clear flags before enabling interrupts

3. Clock errors lead to lost data

In the case of high communication baud rate, if the clock error increases, it is likely to cause data loss.

Solution:

Use a higher precision crystal

Decrease the communication baud rate


Ⅳ. UART sends lost data

Many engineers have experienced lost data transferred through UART, which is typically the cause of the transmission being incomplete.

Although the HAL library has been around for a while, many engineers continue to use the conventional peripheral library. The last byte of data delivered will be lost if the interface is not properly wrapped at this point.

1. UART transmission is not completed resulting in data loss

The following code only considers non-null, but the actual transmission is not completed.

void UART_SendByte(uint8_t Data)

{

while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE));

  USART_SendData(USART1, Data);

}

Sending non-null does not, however, indicate that the sending is finished. It will result in data loss in some circumstances, despite the fact that it is more effective in some.

Use this function, for instance, to put the receiving end device to sleep after transmission or to switch off the power.

Solution:

Wait for sending to complete:

void UART_SendByte(uint8_t Data)

{

while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TXE));

  USART_SendData(USART1, Data);

while(RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC));

}

If you use the standard peripheral library, encapsulate the function according to the actual situation, such as sending timeout.

Or use the HAL-encapsulated interface, and the code includes judging that the transfer is complete:

HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout)

2. Line delay leads to data loss

UART often employs 232 or 485 to lengthen transmissions and boost interference. However, there will be a transmission delay once the data line is too lengthy, particularly when 485 transmits over a great distance and uses the MCU to control the transmission direction.

Solution:

Software adds delay processing

Use the communication protocol to increase the response mechanism

3. Other reasons

Numerous application cases exist for UART. Some of them are used in sophisticated factories where there is a lot of interference, which causes data loss; others are used in places with big temperature swings where there is a lot of clock offset, which causes data loss.

The remedy must be based on the actual circumstance and be directed towards fixing the issue. Use more reliable communication channels, for instance, or implement fault-tolerant processing in software, etc.


UTMEL

We are the professional distributor of electronic components, providing a large variety of products to save you a lot of time, effort, and cost with our efficient self-customized service. careful order preparation fast delivery service

Frequently Asked Questions

1. When two serial ports are used at the same time, how can we ensure that data will not be lost?

1. When debugging the program, you can test it through the virtual serial port. 2. A pair of serial ports are generated and connected to each other to form a serial communication line. 3. In this way, program A opens one port 1 for reading, and program B opens another port 2 for writing.

2. What is the reason for the loss of serial port data in thead.sleep?

The reason for the loss of data is that data reception and data processing are in the same thread. If the data processing time is too long, the data that is too late to receive can only be temporarily stored in the cache. Therefore, once the cache is full, the newly arrived data will flush out the data that cannot be received in the future, resulting in data loss. Therefore, simply increasing the capacity of the cache will not solve the underlying problem of data loss.
Related Articles

  • Discovering New and Advanced Methodology for Determining the Dynamic Characterization of Wide Bandgap Devices
    Discovering New and Advanced Methodology for Determining the Dynamic Characterization of Wide Bandgap Devices
    Saumitra Jagdale15 March 20242180

    For a long era, silicon has stood out as the primary material for fabricating electronic devices due to its affordability, moderate efficiency, and performance capabilities. Despite its widespread use, silicon faces several limitations that render it unsuitable for applications involving high power and elevated temperatures. As technological advancements continue and the industry demands enhanced efficiency from devices, these limitations become increasingly vivid. In the quest for electronic devices that are more potent, efficient, and compact, wide bandgap materials are emerging as a dominant player. Their superiority over silicon in crucial aspects such as efficiency, higher junction temperatures, power density, thinner drift regions, and faster switching speeds positions them as the preferred materials for the future of power electronics.

    Read More
  • Applications of FPGAs in Artificial Intelligence: A Comprehensive Guide
    Applications of FPGAs in Artificial Intelligence: A Comprehensive Guide
    UTMEL29 August 2025527

    This comprehensive guide explores FPGAs as powerful AI accelerators that offer distinct advantages over traditional GPUs and CPUs. FPGAs provide reconfigurable hardware that can be customized for specific AI workloads, delivering superior energy efficiency, ultra-low latency, and deterministic performance—particularly valuable for edge AI applications. While GPUs excel at parallel processing for training, FPGAs shine in inference tasks through their adaptability and power optimization. The document covers practical implementation challenges, including development complexity and resource constraints, while highlighting solutions like High-Level Synthesis tools and vendor-specific AI development suites from Intel and AMD/Xilinx. Real-world applications span telecommunications, healthcare, autonomous vehicles, and financial services, demonstrating FPGAs' versatility in mission-critical systems requiring real-time processing and minimal power consumption.

    Read More
  • Xilinx FPGAs: From Getting Started to Advanced Application Development
    Xilinx FPGAs: From Getting Started to Advanced Application Development
    UTMEL08 September 20259

    This guide is your comprehensive roadmap to understanding and mastering the world of Xilinx FPGA technology. From selecting your first board to deploying advanced AI applications, we'll cover everything you need to know to unlock the potential of these remarkable devices. The global FPGA market is on a significant growth trajectory, expected to expand from USD 8.37 billion in 2025 to USD 17.53 billion by 2035. This surge is fueled by the relentless demand for high-performance, adaptable computing in everything from 5G networks and data centers to autonomous vehicles and the Internet of Things (IoT). This guide will walk you through the key concepts, tools, and products in the Xilinx ecosystem, ensuring you're well-equipped to be a part of this technological revolution.

    Read More
  • Advanced CMOS Devices with Wide Bandgap and Ultrawide Bandgap Technologies
    Advanced CMOS Devices with Wide Bandgap and Ultrawide Bandgap Technologies
    Saumitra Jagdale15 March 20242885

    Power and radio frequency electronics play an increasingly important role in energy-efficient and collaborative future as there is always a demand for faster, smaller, high-voltage and more conductive transistors. Traditionally, silicon has been the semiconductor of choice due to its extensive research and manufacturing history, and natural abundance. While silicon power devices continue to maximize performance, many applications are now integrating wider-band gap semiconductors. These materials offer a significantly higher voltage-conducting capacity, surpassing silicon's limits in tradeoffs related to ON-resistance, capacitances, and breakdown voltage.

    Read More
  • FPGA in Industry and Communication: Key Players, Technologies, and Future Trends
    FPGA in Industry and Communication: Key Players, Technologies, and Future Trends
    UTMEL07 March 20251052

    FPGAs (Field Programmable Gate Arrays) have become the core hardware in the industrial and communication fields due to their programmability and parallel processing capabilities.

    Read More